package team.com;

import java.util.ArrayList;

import team.com.SceneManager.SceneType;

import org.anddev.andengine.engine.camera.hud.HUD;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.input.touch.TouchEvent;
import com.badlogic.gdx.math.Vector2;

import LoadMap.TaiBanDo;
import android.graphics.Color;
import android.hardware.SensorManager;

public class GameScene extends BaseScene implements IOnSceneTouchListener{

	Player _player;
	HUD _gameHUD;
	PhysicsWorld _world;
	TMXTiledMap _tmxTiledMap;
	String _mapName = "map1.tmx";
	TMXLayer _vatCan;
	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		createBackGround();
		loadMap();
		createHUD();
		createPhysics();
		
		setOnSceneTouchListener(this);
	}

	private void loadMap() {
		_tmxTiledMap = TaiBanDo.getTMXTiledMap(this, engine, activity, "map1.tmx", activity);
		ArrayList<TMXLayer> mapLayers = _tmxTiledMap.getTMXLayers();
        for(TMXLayer layer : mapLayers)
        {           
        	if(layer.getName().equals("vatcan"))
        		_vatCan = layer;        	
            attachChild(layer);
        }
        final TMXLayer tmxLayer = _tmxTiledMap.getTMXLayers().get(0);
        camera.setBounds(0, tmxLayer.getWidth(), 0, tmxLayer.getHeight());
        camera.setBoundsEnabled(true);
	}

	private void createPhysics() {		
		_world = new PhysicsWorld(new Vector2(3,  SensorManager.GRAVITY_EARTH), false);
		_player = new Player(10, 10, camera, _world, this);		
		registerUpdateHandler(new IUpdateHandler() {
			
			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				// TODO Auto-generated method stub
				_player.run();
			}
		});
	}

	private void createHUD() {
		// TODO Auto-generated method stub
		_gameHUD = new HUD(1);
		camera.setHUD(_gameHUD);
	}

	private void createBackGround() {
		// TODO Auto-generated method stub
		setBackground(new ColorBackground(Color.BLACK, Color.WHITE, Color.BLUE));		
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_GAME;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		if(pSceneTouchEvent.isActionDown())
			_player.jump();
		return false;
	}

}
